package com.didd.core.message.dingtalk.client;

import com.alibaba.fastjson.JSONObject;
import com.didd.core.message.dingtalk.entity.*;
import com.didd.core.message.dingtalk.exception.DingTalkException;
import com.didd.core.message.dingtalk.type.HideAvatarType;
import com.didd.core.message.dingtalk.type.ResponseCodeType;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.io.IOException;
import java.util.List;

/**
 * 钉钉机器人客户端
 *
 * @author diddd
 * @version 1.0.0
 */
@Slf4j
@Setter
public class DingTalkRobotClient {

    public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

    private static final int OK = 200;
    private static OkHttpClient client = new OkHttpClient();
    private String webHook;

    public DingTalkRobotClient(String webHook) {
        this.webHook = webHook;
    }
    /**
     * 支持外部传入WebHook地址的方式发送消息
     *
     * @param url
     * @param message
     * @return
     */
    public DingTalkResponse sendMessageByURL(String url, BaseMessage message) {
        RequestBody body = RequestBody.create(JSON, JSONObject.toJSONString(message.toMessageMap()));
        Request request = new Request.Builder().url(url).post(body).build();
        try {
            Response response = client.newCall(request).execute();
            if (response.code() == OK) {
                DingTalkResponse dingTalkResponse = JSONObject.parseObject(response.body().string(), DingTalkResponse.class);
                if (!ResponseCodeType.OK.getValue().equals(dingTalkResponse.getErrcode())) {
                    log.error("dingTalk request fail: {}", dingTalkResponse.toString());
                    throw new DingTalkException(dingTalkResponse.getErrcode(), dingTalkResponse.getErrmsg());
                }
                return dingTalkResponse;
            }
        } catch (IOException e) {
            log.error("dingTalk request fail", e);
        }
        return null;
    }

    /**
     * 发送WebHook消息到钉钉
     *
     * @param message
     * @return
     */
    public DingTalkResponse sendMessage(BaseMessage message) {
        return this.sendMessageByURL(webHook, message);
    }

    /**
     * 发送文本消息到钉钉
     *
     * @param message
     * @return
     */
    public DingTalkResponse sendTextMessage(TextMessage message) {
        return this.sendMessage(message);
    }

    /**
     * 发送文本消息到钉钉
     *
     * @param content
     * @return
     */
    public DingTalkResponse sendTextMessage(String content) {
        TextMessage message = TextMessage.builder().content(content).build();
        return this.sendMessage(message);
    }

    /**
     * 发送文本消息到钉钉
     *
     * @param content
     * @param atMobiles
     * @return
     */
    public DingTalkResponse sendTextMessage(String content, String[] atMobiles) {
        TextMessage message = TextMessage.builder().content(content).atMobiles(atMobiles).build();
        return this.sendMessage(message);
    }

    /**
     * 发送文本消息到钉钉
     *
     * @param content
     * @param isAtAll
     * @return
     */
    public DingTalkResponse sendTextMessage(String content, boolean isAtAll) {
        TextMessage message = TextMessage.builder().content(content).isAtAll(isAtAll).build();
        return this.sendMessage(message);
    }

    /**
     * 发送Link消息到钉钉
     *
     * @param message
     * @return
     */
    public DingTalkResponse sendLinkMessage(LinkMessage message) {
        return this.sendMessage(message);
    }

    /**
     * 发送Link消息到钉钉
     *
     * @param title
     * @param text
     * @param messageUrl
     * @return
     */
    public DingTalkResponse sendLinkMessage(String title, String text, String messageUrl) {
        LinkMessage message = LinkMessage.builder().title(title).text(text).messageUrl(messageUrl).build();
        return this.sendMessage(message);
    }

    /**
     * 发送Link消息到钉钉
     *
     * @param title
     * @param text
     * @param messageUrl
     * @param picUrl
     * @return
     */
    public DingTalkResponse sendLinkMessage(String title, String text, String messageUrl, String picUrl) {
        LinkMessage message = LinkMessage.builder().title(title).text(text).messageUrl(messageUrl).picUrl(picUrl).build();
        return this.sendMessage(message);
    }

    /**
     * 发送MarkDown消息到钉钉
     *
     * @param message
     * @return
     */
    public DingTalkResponse sendMarkdownMessage(MarkdownMessage message) {
        return this.sendMessage(message);
    }

    /**
     * 发送MarkDown消息到钉钉
     *
     * @param title
     * @param text
     * @return
     */
    public DingTalkResponse sendMarkdownMessage(String title, String text) {
        MarkdownMessage message = MarkdownMessage.builder().title(title).text(text).build();
        return this.sendMessage(message);
    }

    /**
     * 发送MarkDown消息到钉钉
     *
     * @param title
     * @param text
     * @param atMobiles
     * @return
     */
    public DingTalkResponse sendMarkdownMessage(String title, String text, String[] atMobiles) {
        MarkdownMessage message = MarkdownMessage.builder().title(title).text(text).atMobiles(atMobiles).build();
        return this.sendMessage(message);
    }

    /**
     * 发送MarkDown消息到钉钉
     *
     * @param title
     * @param text
     * @param isAtAll
     * @return
     */
    public DingTalkResponse sendMarkdownMessage(String title, String text, boolean isAtAll) {
        MarkdownMessage message = MarkdownMessage.builder().title(title).text(text).isAtAll(isAtAll).build();
        return this.sendMessage(message);
    }

    /**
     * 发送ActionCard消息到钉钉
     *
     * @param message
     * @return
     */
    public DingTalkResponse sendActionCardMessage(ActionCardMessage message) {
        return this.sendMessage(message);
    }

    /**
     * 发送ActionCard消息到钉钉
     *
     * @param title
     * @param text
     * @return
     */
    public DingTalkResponse sendActionCardMessage(String title, String text) {
        ActionCardMessage message = ActionCardMessage.builder().title(title).text(text).build();
        return this.sendMessage(message);
    }

    /**
     * 发送ActionCard消息到钉钉
     *
     * @param title
     * @param text
     * @param hideAvatar
     * @return
     */
    public DingTalkResponse sendActionCardMessage(String title, String text, HideAvatarType hideAvatar) {
        ActionCardMessage message = ActionCardMessage.builder().title(title).text(text).hideAvatar(hideAvatar).build();
        return this.sendMessage(message);
    }

    /**
     * 发送ActionCard消息到钉钉
     *
     * @param title
     * @param text
     * @param button
     * @return
     */
    public DingTalkResponse sendActionCardMessage(String title, String text, ActionCardButton button) {
        ActionCardMessage message = ActionCardMessage.builder().title(title).text(text).build();
        message.addButton(button);
        return this.sendMessage(message);
    }

    /**
     * 发送ActionCard消息到钉钉
     *
     * @param title
     * @param text
     * @param hideAvatar
     * @param button
     * @return
     */
    public DingTalkResponse sendActionCardMessage(String title, String text, HideAvatarType hideAvatar, ActionCardButton button) {
        ActionCardMessage message = ActionCardMessage.builder().title(title).text(text).hideAvatar(hideAvatar).build();
        return this.sendMessage(message);
    }

    /**
     * 发送FeedCard消息到钉钉
     *
     * @param feedCardMessage
     * @return
     */
    public DingTalkResponse sendFeedCardMessage(FeedCardMessage feedCardMessage) {
        return this.sendMessage(feedCardMessage);
    }

    /**
     * 发送FeedCard消息到钉钉
     *
     * @param feedCardItems
     * @return
     */
    public DingTalkResponse sendFeedCardMessage(List<FeedCardMessageItem> feedCardItems) {
        FeedCardMessage message = FeedCardMessage.builder().feedCardItems(feedCardItems).build();
        return this.sendMessage(message);
    }
}